package com.liang.maogouyuyin.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.liang.maogouyuyin.bean.ExpenditureBean
import com.liang.maogouyuyin.local.LocalRepository
import kotlinx.coroutines.launch

class ExpenditureViewModel: ViewModel() {
    private val _expenditureList = MutableLiveData<ArrayList<ExpenditureBean>>().apply { value = ArrayList() }
    val expenditureList get() = _expenditureList

    fun saveExpenditure(name: String, money: Float, content: String, date: Long) {
        val bean = ExpenditureBean(
            name, money, content, date
        )
        viewModelScope.launch {
            LocalRepository.saveExpenditure(bean)
        }
    }

    fun deleteExpenditure(bean: ExpenditureBean) {
        viewModelScope.launch {
            LocalRepository.deleteExpenditure(bean)
        }
    }

    fun updateExpenditure(bean: ExpenditureBean) {
        viewModelScope.launch {
            LocalRepository.updateExpenditure(bean)
        }
    }

    fun loadExpenditureByName(name: String) {
        viewModelScope.launch {
            _expenditureList.value = LocalRepository.loadExpenditure(name)
        }
    }
}